package com.wendy.web.controller;

import com.wendy.web.editor.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhuwending
 */
@Controller
public class BaseController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new XssEditor());
        binder.registerCustomEditor(Date.class, new DateEditor());
        binder.registerCustomEditor(BigDecimal.class, new BigDecimalEditor());
        binder.registerCustomEditor(Long.class, new LongEditor());
        binder.registerCustomEditor(Integer.class, new IntegerEditor());
    }

    protected Map<String, Object> requestParam2Map(HttpServletRequest request) {
        Enumeration<String> enumeration = request.getParameterNames();
        HashMap<String, Object> map = new HashMap<>(5);
        while (enumeration.hasMoreElements()) {
            String paramName = enumeration.nextElement();
            map.put(paramName, request.getParameter(paramName));
        }
        return map;
    }

    public String redirect(String s) {
        return "redirect:" + s;
    }

    public String forward(String s) {
        return "forward:" + s;
    }
}
